//
//  GDBuyCarLL.m
//  美团试验 (自己)
//
//  Created by 热带雨林 on 17/2/14.
//  Copyright © 2017年 热带雨林. All rights reserved.
//

#import "GDBuyCarLL.h"
#import "CZAdditions.h"
#import "Masonry.h"
#import "GDDetailsFoodModel.h"

@interface GDBuyCarLL ()

@property(nonatomic, weak)UIButton *showButton;
@property(nonatomic, weak)UILabel *inforLabel;
@property(nonatomic, weak)UIButton *payButton;
@property(nonatomic, weak)UIButton  *LittleredButton;

@end

@implementation GDBuyCarLL

- (void)setupUI{
    self.backgroundColor = [UIColor whiteColor];
    
    //购物车
    UIButton *showButton = [[UIButton alloc]init];
    self.showButton = showButton;
    [showButton setBackgroundImage:[UIImage imageNamed:@"button_shoppingCart_empty"] forState:UIControlStateNormal];
    
    [showButton sizeToFit];
    
    [self addSubview:showButton];

    [showButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(40);
        make.top.equalTo(self).offset(-30);
        make.left.equalTo(self).offset(5);
    }];
    
    [showButton addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];

    //显示Label
    UILabel *inforLabel = [[UILabel alloc]init];
    inforLabel.font = [UIFont systemFontOfSize:14];
    
    self.inforLabel = inforLabel;
    [self addSubview:inforLabel];
    
    [inforLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(showButton.mas_right).offset(40);
        make.top.equalTo(self).offset(15);
    }];
    
    //计算按钮
    UIButton *payButton = [UIButton cz_textButton:@"还差20" fontSize:14 normalColor:[UIColor whiteColor] selectedColor:[UIColor blackColor]];
    self.payButton = payButton;
    [payButton setBackgroundColor:[UIColor grayColor]];
    
    [payButton addTarget:self action:@selector(clickButton) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:payButton];
    
    [payButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(46);
        make.width.mas_equalTo(100);
        make.top.right.equalTo(self);
    }];
    
    //小红点
    UIButton *LittleredButton = [[UIButton alloc]init];
    LittleredButton.hidden = YES;
    self.LittleredButton = LittleredButton;
    
    [LittleredButton setBackgroundImage:[UIImage imageNamed:@"icon_food_count_bg"] forState:UIControlStateNormal];
    
    [self addSubview:LittleredButton];
    
    [LittleredButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.width.mas_equalTo(20);
        make.top.equalTo(self).offset(-30);
        make.left.equalTo(self).offset(37);
    }];
    
    UIView *lineView = [[UIView alloc]init];
    lineView.backgroundColor = [UIColor grayColor];
    [self addSubview:lineView];
    
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.left.equalTo(self).offset(50);
        make.width.mas_equalTo([UIScreen mainScreen].bounds.size.width - 50);
        make.height.mas_equalTo(0.2);
    }];
}

- (void)setBuyList:(NSArray<GDDetailsFoodModel *> *)buyList{
    _buyList = buyList;
    _LittleredButton.hidden = (buyList.count == 0);
    //设置购物车的选中壮态
    _showButton.selected = !(buyList.count == 0);
    _showButton.enabled = !(buyList.count == 0);
    
    __block NSInteger allCount = 0;
    __block CGFloat allPrice = 0;
    
    //遍历数组 根据buyCount的个数
    [buyList enumerateObjectsUsingBlock:^(GDDetailsFoodModel * _Nonnull model, NSUInteger idx, BOOL * _Nonnull stop) {
        
        allCount += model.buyCount;
        
        allPrice += model.buyCount * model.min_price.floatValue;
        
    }];
    
    [_LittleredButton setTitle:@(allCount).description forState:UIControlStateNormal];
    
    if(allCount > 0){
        [_showButton setImage:[UIImage imageNamed:@"button_shoppingCart_noEmpty"] forState:UIControlStateNormal];
    }
    if(allPrice){
        [_inforLabel setTextColor:[UIColor redColor]];
        _inforLabel.text = [NSString stringWithFormat:@"%.2f",allPrice];
    }else{
        [_inforLabel setTextColor:[UIColor lightGrayColor]];
        self.inforLabel.text = @"购物车空空如也";
    }
    
    CGFloat minPrice = allPrice - 20;
    if(minPrice > 0){
        [_payButton setBackgroundColor:[UIColor orangeColor]];
        [_payButton setTitle:@"计算" forState:UIControlStateNormal];
    }else{
        [_payButton setBackgroundColor:[UIColor grayColor]];
        NSString *str = [NSString stringWithFormat:@"还差%.2f",ABS(minPrice)];
        [_payButton setTitle:str forState:UIControlStateNormal];
    }
    //给购物车添加一个动画
    _showButton.transform = CGAffineTransformMakeScale(0.8, 0.8);
    [UIView animateWithDuration:2 delay:0 usingSpringWithDamping:0.3 initialSpringVelocity:0 options: UIViewAnimationOptionAllowUserInteraction animations:^{
        _showButton.transform = CGAffineTransformIdentity;
    } completion:nil];

}

- (void)click{
    
    if([_delegate respondsToSelector:@selector(GDBuyCarController:withSelectedArray:)]){
        [_delegate GDBuyCarController:self withSelectedArray:_buyList];
    }
}

- (void)clickButton{
    
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示信息" message:@"支付功能没有完全实现,请谅解" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alertView show];
}


@end
